var app = getApp();
const apiHost = require('../../../../common/api_host.js');
var utils = require('../../../../utils/utils.js');
var httpJson=require('../../../../utils/http_json.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    choose:'0',
    pageNum:0,
    pageSize:10,
    totalCount:0,
    topTotalCount:0,    
    list:[],
    hasMore:false,
    contentPageNum:0,topContentPageNum:0,
   
  
    avatar:"http://www.pptbz.com/pptpic/UploadFiles_6909/201203/2012031220134655.jpg"

  },
  // toupper :function(){
   
  //   // wx.getStorageSync("roomId",);
  //   // wx.getStorageSync("name",);
  //   this.setData({
  //     roomId:wx.getStorageSync("roomId"),
  //     name: wx.getStorageSync("name"),
  //     contentPageNum:0,
  //     topContentPageNum:0
  //   })
  //   wx.showLoading({

  //     title: '正在刷新',
  //   })
  //   this.getlist(0);
  //   this.getRoomDetail()
  // },
  addLikeContent:function(e){
    let contentId=app.E(e,"myid");
    let userLike=app.E(e,'islike');
    let index=app.E(e,'idx');
    console.log(contentId,userLike,index)
    var  url;
    if(userLike){
      utils.successToast('取消惊叹成功',500)
      url =`${apiHost.config.portalApiHost}/portal/content/removeLike`;
      let data={contentId}
      httpJson.post(url,data,res=>{
        if(res.data.errorCode==0){
          
          var params={};
          if(this.data.choose==0){
            var str=`list[${index}].userLike`;
            
            this.data.list[index].likeNum--
            this.setData({
              list:this.data.list
            })
          }
          if(this.data.choose==1){
            var str=`TopList[${index}].userLike`;
            //1.1start：这里时惊叹数量的变化
            this.data.TopList[index].likeNum--
            this.setData({
              TopList:this.data.TopList
            })
            //1.1end
          }
          console.log('str~~',str)
          params[str]=false;
         
         this.setData(params)
        }
      })
    }
    if(!userLike){
      utils.successToast('惊叹成功',500)
      url =`${apiHost.config.portalApiHost}/portal/content/addLike`;
      let data={contentId}
      httpJson.post(url,data,res=>{
        if(res.data.errorCode==0){
         
          var params={};
          if(this.data.choose==0){
            var str=`list[${index}].userLike`;
            this.data.list[index].likeNum++
           
            this.setData({
              list:this.data.list
            })
          }
          if(this.data.choose==1){
            var str=`TopList[${index}].userLike`;
            this.data.TopList[index].likeNum++
            this.setData({
              TopList:this.data.TopList
            })
          }
          params[str]=true;
         
         this.setData(params)
        
        }
      })
    }
  },
  addLikeRoom:function(){
     var  url;
    if(this.data.userLike){
      url =`${apiHost.config.portalApiHost}/portal/aboveroom/removeLike`;
      utils.successToast('取消惊叹成功',500)
      
      this.data.likeNum--
      
    }
    if(!this.data.userLike){
      utils.successToast('惊叹成功',500)
      url =`${apiHost.config.portalApiHost}/portal/aboveroom/addLike`;
      this.data.likeNum++
    }
    let data={
      roomId:this.data.roomId
    }
    httpJson.post(url,data,res=>{
   
      if(res.data.errorCode==0){
        this.setData({
          userLike:!this.data.userLike,
          likeNum:this.data.likeNum
        })
       
      }
    })
  },
  add:function(e){
    wx.navigateTo({
      url: '../add/add?myid='+e.currentTarget.dataset.myid
    })

  },
  change_choose:function(e){
    // if(e.currentTarget.dataset.myid=='0'){
      // 切换的时候把是否有内容的数据清空
      this.setData({
        choose:e.currentTarget.dataset.myid,
       

      })
      if(!this.data.TopList&&this.data.choose==1){
        
        // if(this.data.choose==1){
          this.getlist(0,true)
        // }
      }
      

  },
  toDetail:function(e){
    //点击区域时转发的话就不跳转到详情页面
   
    if(e.target.dataset.reply==1){
      return;
    }
    let id=app.E(e,'myid')
    wx.navigateTo({
      url: '../detail/detail?id='+id
    })
  },
  getlist:function(pageNum,isTop){
    var  url=`${apiHost.config.portalApiHost}/portal/aboveroom/content`;
    let data={
      pageNum,pageSize:10,roomId:this.data.roomId
    }
    if(isTop){
      data.isTop=true
    }
    console.log(isTop)
    httpJson.get(url,data,res=>{
      // debugger
      if(res.data.errorCode==0){
        setTimeout(()=>{
          wx.hideLoading()
          wx.stopPullDownRefresh()
        },1000)
       
        console.log(isTop)
        if(!isTop){

          if(pageNum==0){
            this.data.list=res.data.body.rows
          }
          /**判断是否还有数据 */
          if(res.data.body.totalPage==this.data.contentPageNum+1){
            this.setData({
              contentHasMore:false
            })
  
          }
          /**此时进行分页处理 */
          if(data.pageNum>0){
            this.data.list= this.data.list.concat(res.data.body.rows);
          }
          //为了显示暂无药丸
          if(res.data.body.rows.length==0){
            // debugger
            this.setData({
              showNoList:true,
              
              topTotalCount:0
            })
            return;
          }

          
          let list=res.data.body.rows
          
          list.forEach((ele)=>{
            ele.mytime=utils.formatDate2(ele.createdTime);
          })

         
         
          this.setData({
            totalCount:res.data.body.totalCount,
            list: this.data.list
          })
        }
        if(isTop){
          if(pageNum==0){
            this.data.TopList=res.data.body.rows
          }
          /**判断是否还有数据 */
          if(res.data.body.totalPage==this.data.topContentPageNum+1){
            this.setData({
              topContentHasMore:false
            })
  
          }
          /**此时进行分页处理 */
          if(data.pageNum>0){
            this.data.TopList= this.data.TopList.concat(res.data.body.rows);
          }


          //为了显示暂无药丸
          if(res.data.body.rows.length==0){
            this.setData({
              showNoToplist:true,
             
              
            })
            return;
          }

          // debugger
          let TopList=res.data.body.rows
        
          TopList.forEach((ele)=>{
            ele.mytime=utils.formatDate2(ele.createdTime);
          })
          // debugger
          this.setData({
            topTotalCount:res.data.body.totalCount,
            TopList:this.data.TopList
          })
        }
       
      }
    })
  },
  getRoomDetail:function(){
    var  url=`${apiHost.config.portalApiHost}/portal/aboveroom/detail/${this.data.roomId}`;
   
    httpJson.get(url,{},res=>{
      if(res.data.errorCode==0){
        this.setData({
          roomName:res.data.body.name,
          likeNum:res.data.body.likeNum,
          userLike:res.data.body.userLike,
          roomtitle:res.data.body.content,

        })
      }
    })

  },
  scrolltolower :function(){
     /**判断当前是否加载完毕 */
     //选择了全部药丸
     if(this.data.choose==0){
          if(this.data.list.length<this.data.totalCount){
            this.setData({
              contentHasMore:true
            })
          
           let pageNum=++this.data.contentPageNum
            this.getlist(pageNum)
          }

     }
     //选择了特殊药丸
     if(this.data.choose==1){

      if(this.data.TopList.length<this.data.topTotalCount){
        this.setData({
          topContentHasMore:true
        })
        let pageNum=++this.data.topContentPageNum
        this.getlist(pageNum,1)
      }
       

    }
     
  },
  
  formSubmit: function(e) {


    var formId = e.detail.formId;
  
    let path = `${apiHost.config.portalApiHost}/portal/userformId/add`;
    httpJson.post(path, { 'formId': formId}, function (data) {
      let res = data.data;
     
    })
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("我了了")
   
    app.verifyLogin()
    // app.getuser(this)
    wx.showLoading({
      title: '加载中',
    })
    console.log(options,"options")
    this.setData({
      roomId:options.id,
      name:options.name
    })
    wx.setStorageSync("roomId",options.id);
    wx.setStorageSync("name",options.name);
    this.getlist(0);
    this.getRoomDetail()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      roomId:wx.getStorageSync("roomId"),
      name: wx.getStorageSync("name"),
      contentPageNum:0,
      topContentPageNum:0,
      showNoList:false,
    })
  
    this.getlist(0);
    this.getRoomDetail()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
    this.setData({
      roomId:wx.getStorageSync("roomId"),
      name: wx.getStorageSync("name"),
      contentPageNum:0,
      topContentPageNum:0
    })
    wx.showLoading({

      title: '正在刷新',
    })
    this.getlist(0);
    this.getRoomDetail()
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
   
  },


  /**
   * 用户点击右上角分享
   */
  
  onShareAppMessage(res) {
    app.share()
    if (res.from === 'button') {
      var content=res.target.dataset.content
     
      if(content.length>50){
        content=content.substr(0,50)+'...'
      }
      var title=content
    }
    if(res.from === 'menu'){
      var imageUrl="/images/index/reply/reply.png";
      var title='我正在惊人院，邀请你来……'
    }
    return {
      title,
      imageUrl
      
    }
  }
})